﻿@using Composite.Search.SimplePageSearch
@inherits RazorFunction
@functions {
    public override string FunctionDescription
    {
        get { return "Search results rendering"; }
    }

    [FunctionParameter(Label = "Show Search Form", Help = "If checked, a search form will be shown above the search result.", DefaultValue = "true")]
    public bool ShowSearchForm { get; set; }

    [FunctionParameter(Label = "Search Form Button Label", Help = "The text to show on the search form submit button. When left blank a generic search icon is used.", DefaultValue = "")]
    public string SearchButtonLabel { get; set; }

    [FunctionParameter(Label = "Results Per Page", Help = "The maximum number of results to show at a time. If the search contains more results links will be provided to navigate results.", DefaultValue = "10")]
    public int PageSize { get; set; }

    [FunctionParameter(Label = "Search Current Site Only", Help = "If checked only the current site will be searched. Uncheck to search all subsites.", DefaultValue = "true")]
    public bool CurrentSite { get; set; }

    [FunctionParameter(Label = "Show Descriptions", Help = "If checked search results will have descriptions (when available) shown below the title.", DefaultValue = "false")]
    public bool ShowDescriptions { get; set; }

   

    private int TotalCount;
    private int PageNumber
    {
        get
        {
            int page;
            return int.TryParse(Request["p"], out page) && page >= 1 && page <= TotalPages ? page : 1;
        }
    }

    protected int TotalPages
    {
        get { return (int)Math.Ceiling((decimal)TotalCount / PageSize); }
    }

}

@{

    var searchQuery = Request.QueryString["q"] ?? string.Empty;

    var searchResults = new List<SearchResultEntry>();

    if (!string.IsNullOrWhiteSpace(searchQuery) && searchQuery.Length > 1)
    {
        // Tokenize
        searchQuery = searchQuery.Trim().ToLower();
        string[] keywords = searchQuery.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

        var pages = SearchFacade.Search(keywords, CurrentSite);
        
        TotalCount = pages.Count;

        searchResults = pages.Skip(PageSize * (PageNumber - 1)).Take(PageSize).ToList();
    }
}

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://www.composite.net/ns/function/1.0">
<head>
</head>
<body>
    <div class="simplepagesearch clearfix">
        @if (ShowSearchForm)
        {
            <div class="clearfix">
                @Html.C1().Function("Composite.Search.SimplePageSearch.SearchForm", 
									new { SearchResultPage = new DataReference<IPage>(CurrentPageNode.Id),
										  SearchButtonLabel = SearchButtonLabel})
            </div>
            if (!string.IsNullOrWhiteSpace(searchQuery)) { <hr/> }
        }
        @if (!string.IsNullOrWhiteSpace(searchQuery))
        {
            <p>
                @Html.Raw(string.Format(Resources.SimplePageSearch.SearchResults_TotalCount, TotalCount, searchQuery))
            </p>
        }
        <ol class="simplepagesearch-results" start="@(PageSize*(PageNumber-1)+1)">
            @foreach (var entry in searchResults)
            {
                <li>
                    <a href="@entry.Url">
                        @entry.Title
                    </a>
					@if (ShowDescriptions && !string.IsNullOrEmpty(entry.Description))
					{
						<br />
						@entry.Description
					}
                </li>
            }
        </ol>
        @if (TotalPages > 1)
        {
            <ul class="pagination">
                @for (int i = 1; i <= TotalPages; i++)
                {
                    if (i == PageNumber)
                    {
                        <li class="active"><span>@i</span></li>
                    }
                    else
                    {
                        <li><a href="?p=@i&amp;q=@searchQuery">@i</a></li>
                    }
                }
            </ul>
        }
    </div>
</body>
</html>